package com.bzkj.inventory.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.inventory.service.InventoryHandleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class InventoryHandleServiceImpl implements InventoryHandleService {

    //    现存量查询path
    private static final String path_queryInventory = "/nccloud/api/ic/onhand/onhandQuery";
    //    可用量查询path
    private static final String path_queryAvailability = "/nccloud/api/ic/atp/atpQuery";
    //    库存状态调整path
    private static final String path_statusAdjustment = "/nccloud/api/ic/onhand/stateAdjust";
    @Value("${ncc.baseUrl}")
    private String baseUrl;
    @Value("${ncc.sourceSystem}")
    private static String sourceSystem;

    @Value("${ncc.targetSystem}")
    private static String targetSystem;

    @Value("${ncc.serviceName}")
    private static String serviceName;

    @Value("${ncc.contentType}")
    private static String contentType;

    @Override
    public AjaxResult queryInventoryQuery(String cmaterialoid, String cstateid, String cwarehouseid, String vbatchcode, String dplandate) {
        HttpResponse response;
        if (dplandate != null && !dplandate.isEmpty()) {
            //可用量查询
            response = queryAvailability(cmaterialoid, dplandate, vbatchcode);
        } else {
            //现存量查询
            response = queryInventory(cmaterialoid, cstateid, cwarehouseid, vbatchcode);
        }
        AjaxResult ajaxResult = new AjaxResult();
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            Boolean success = entries.getBool("success");
            String message = entries.getStr("message");
            if (success) {
                String data = entries.getStr("data");
                String code = entries.getStr("code");
                return ajaxResult.put("data", data).put("code", code).put("message", message).put("success", success);
            } else {
                return ajaxResult.put("message", message).put("success", success);
            }

        }
        return AjaxResult.error();
    }

    public HttpResponse queryAvailability(String cmaterialoid, String dplandate, String vbatchcode) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("cmaterialoid", cmaterialoid);
        hashMap.put("cstateid", dplandate);
        hashMap.put("cwarehouseid", vbatchcode);
        hashMap.put("pk_org", "111");
        String jsonString = JSONUtil.toJsonStr(hashMap);

        // 添加请求头
        HttpResponse response = HttpUtil.createPost(baseUrl)
                .header("sourceSystem", sourceSystem)
                .header("targetSystem", targetSystem)
                .header("serviceName", serviceName)
                .header("contentType", contentType)
                .header("path", path_queryAvailability)
                .body(jsonString, "application/json")
                .execute();
        return response;
    }

    public HttpResponse queryInventory(String cmaterialoid, String cstateid, String cwarehouseid, String vbatchcode) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("cmaterialoid", cmaterialoid);
        hashMap.put("cstateid", cstateid);
        hashMap.put("cwarehouseid", cwarehouseid);
        hashMap.put("pk_org", "111");
        hashMap.put("vbatchcode", vbatchcode);
        String jsonString = JSONUtil.toJsonStr(hashMap);

        // 添加请求头
        HttpResponse response = HttpUtil.createPost(baseUrl)
                .header("sourceSystem", sourceSystem)
                .header("targetSystem", targetSystem)
                .header("serviceName", serviceName)
                .header("contentType", contentType)
                .header("path", path_queryInventory)
                .body(jsonString, "application/json")
                .execute();
        return response;
    }

    @Override
    public AjaxResult statusAdjustment(String cadjuststateid, String cmaterialoid, String cstateid, String vbatchcode, String nadjustnum) {
        HttpResponse response = statusAdjustment(cadjuststateid, cmaterialoid, cstateid, vbatchcode, nadjustnum, "");
        AjaxResult ajaxResult = new AjaxResult();
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            Boolean success = entries.getBool("success");
            String message = entries.getStr("message");
            return ajaxResult.put("success", success).put("message", message);
        }
        return AjaxResult.error();
    }

    public HttpResponse statusAdjustment(String cadjuststateid, String cmaterialoid, String cstateid, String vbatchcode, String nadjustnum, String pk_org) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("cadjuststateid", cadjuststateid);
        hashMap.put("cmaterialoid", cmaterialoid);
        hashMap.put("cstateid", cstateid);
        hashMap.put("vbatchcode", vbatchcode);
        hashMap.put("nadjustnum", nadjustnum);
        hashMap.put("pk_org", "111");
        String jsonString = JSONUtil.toJsonStr(hashMap);

        // 添加请求头
        HttpResponse response = HttpUtil.createPost(baseUrl)
                .header("sourceSystem", sourceSystem)
                .header("targetSystem", targetSystem)
                .header("serviceName", serviceName)
                .header("contentType", contentType)
                .header("path", path_statusAdjustment)
                .body(jsonString, "application/json")
                .execute();
        return response;
    }
}
